import React, { useRef } from 'react'
import "./index.css"
import { Button } from 'antd-mobile'
import { useNavigate } from 'react-router-dom'
import Shou from '../Shou'
import Zhang from '../Zhang'
import Ren from '../Ren'
import axios from 'axios'
// const axiosInstance = axios.create();

// axiosInstance.interceptors.request.use((config) => {
//     const shortToken = 'your_short_token_here';
//     config.headers['Authorization'] = `Bearer ${shortToken}`;
//     return config;
// });
export default function Login() {
    let navigate = useNavigate()
    let phoneref = useRef()
    let yanzhengmaref = useRef()
    let [idnex, setindex] = React.useState(0)
    let yanzhengma = () => {
        let phone = phoneref.current.value
        // 正则表达式的手机号判断
        let reg = /^1[3456789]\d{9}$/
        if (reg.test(phone)) {
            axios.post("/yzk/get_sms", { phone })
        } else {
            alert("请输入正确的手机号")
        }
        // axios.post("/get_sms")
    }
    let login = async () => {
        let yanzhengma = yanzhengmaref.current.value
        let {code,data,id,token} = (await axios.post("/yzk/login_phone", { phone:phoneref.current.value,yanzhengma })).data

        if (code === 403) {
            alert("验证码错误")
            return
        } else {
            // setindex(1)
            sessionStorage.setItem("id",id)

            navigate("/main")
            // sessionStorage.setItem("user", JSON.stringify(data)||'')


            navigate("/bttm/main")
        
            sessionStorage.setItem("users", JSON.stringify((await axios.post("/yzk/login_phone", { phone:phoneref.current.value,yanzhengma })).data.data))

            let { accessToken, refreshToken } = token
            // 将token存储到本地
            localStorage.setItem("AccessToken", accessToken)
            localStorage.setItem("RefreshToken", refreshToken)
            alert("登陆成功")
        }
    }
    return (
        <div className='login'>
            <div className='login_top'>
                <span className='login_title'>OMINI.cn</span>
                <input ref={phoneref} className='int' placeholder='手机号'>
                </input>
                {/* 校验 */}
                <input ref={yanzhengmaref} className='int' placeholder='验证码'>
                </input>

                <div onClick={yanzhengma} className='btnn'>
                    验证码
                </div>

                <div onClick={login} className='login_btn'>
                    登 录
                </div>
            </div>
        </div>
    )
}
